package.cpath = "luaclib/?.so;3rd/lua-cjson/?.so;"
local skynet = require "skynet"
local socket = require "socket"
require "skynet.manager"	-- import skynet.register
local mysql = require "mysql"
local redis   = require "redis"
local json   = require "cjson"
local httpc = require "http.httpc"	
local dns = require "dns"

require "config_redis"

CMD = {}

local HALL_KEY = {}  
HALL_KEY.hall_service = 'service'      --房间service_name
HALL_KEY.hall_room_service_name = 'room_service_name'      --房间service_name
HALL_KEY.service_list = 'new_service_list'      --房间service_name
HALL_KEY.cost_card_0 = 'cost_card_0'      --房间service_name
HALL_KEY.cost_card_1 = 'cost_card_1'      --房间service_name
HALL_KEY.cost_card_2 = 'cost_card_2'      --房间service_name

local redis_db 
local function start_redis(args)
	redis_db = redis.connect(args)
end

---------------------------------begin redis function---------------------------------

function CMD.redis_get(key)   
    return redis_db:get( key ) 
end


--del  key 
function CMD.redis_del(key) 
    return redis_db:del( key )  
end

--hget  key 
function CMD.redis_hgetall(key) 
    local data = redis_db:hgetall( key )  
    local rs = {}
    if data ~= nil then  
        for k,v in pairs(data) do
            if k % 2 == 1 then
                rs[v] = data[k+1]
            end 
        end
    end
    return rs
end

--hget  key 
function CMD.redis_hget(key,feid) 
    return redis_db:hget( key,feid )  
end
--hset  key 
function CMD.redis_hset(key,feid,val) 
    return redis_db:hset( key,feid,val )  
end

--del  key 
function CMD.redis_hdel(key,feid) 
    return redis_db:hdel( key,feid )  
end

--lpush  在名称为key的list头添加一个值为value的 元素 
function CMD.redis_lpush(key,value) 
    return redis_db:lpush( key,value )  
end

--返回名称为key的list的长度
function CMD.redis_llen(key ) 
    return redis_db:lpush( key  )  
end

--返回名称为key的list中start至end之间的元素（下标从0开始
function CMD.redis_lrange(key, start, endnum ) 
    return redis_db:lrange( key, start, endnum  )  
end

--返回名称为key的list中start至end之间的元素（下标从0开始
function CMD.redis_ltrim(key, start, endnum ) 
    return redis_db:ltrim( key, start, endnum  )  
end

--根据参数 count 的值，移除列表中与参数 value 相等的元素。
function CMD.redis_lrem(key, count , value ) 
    return redis_db:lrem( key, count , value  )  
end



---------------------------------begin hall function--------------------------------- 
 

function CMD.get_service(id) 
    local room_id = math.floor(id)
	if room_id < 110000 then 
		return cur_service_name--返回域名
	end
    local service_name =  'epenli.vicp.cc:8888'
    return service_name
end 

function CMD.del_service(id)
	local room_id = math.floor(id)
	redis_db:hdel(HALL_KEY.hall_room_service_name,room_id) 
end

function CMD.split(str, delimiter)
	if str==nil or str=='' or delimiter==nil then
		return nil
	end
	
    local result = {}
    for match in (str..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end

function CMD.get_curr_service(service)
	local ips = redis_db:hget(HALL_KEY.service_list, cur_server_id)
	local arr = CMD.split(ips,",")
	for i,v in ipairs(arr) do 
		if v == service then 
			return true
		end
	end
	return false,arr[1]
end 
 


skynet.start(function()
	-- start_redis(redis_conf)
	skynet.dispatch("lua", function(_,_, command, ...)
		local f = CMD[command]
		skynet.ret(skynet.pack(f(...)))
	end)
	skynet.register "HALL"
end)

